<?php

namespace app\common\model\settings;

use think\facade\Cache;
use app\common\model\BaseModel;

/**
 * 系统设置模型
 */
class Setting extends BaseModel
{
    protected $name = 'setting';
    protected $createTime = false;

    /**
     * 获取器: 转义数组格式
     */
    public function getValuesAttr($value)
    {
        return json_decode($value, true);
    }

    /**
     * 修改器: 转义成json格式
     */
    public function setValuesAttr($value)
    {
        return json_encode($value);
    }

    /**
     * 获取指定项设置
     */
    public static function getItem($key, $app_id = null)
    {
        $data = self::getAll($app_id);
        $data_key = $data[$key];
        if (isset($data_key)) {
            $data_key = $data[$key]['values'];
            jsonRecursive($data_key);
        } else {
            $data_key = [];
        }

        return $data_key;
    }

    /**
     * 获取设置项信息
     */
    public static function detail($key)
    {
        return self::where('key', '=', $key)->find();
    }

    /**
     * 全局缓存: 系统设置
     */
    public static function getAll($app_id = null)
    {
        $static = new static;
        is_null($app_id) && $app_id = $static::$app_id;

        if (!$data = Cache::get('setting_' . $app_id)) {
            $setting = $static->where(compact('app_id'))->select();
            $data = empty($setting) ? [] : array_column($static->collection($setting)->toArray(), null, 'key');
            Cache::tag('cache')->set('setting_' . $app_id, $data);
        }
        return $static->getMergeData($data);
    }

    /**
     * 数组转换为数据集对象
     */
    public function collection($resultSet)
    {
        $item = current($resultSet);
        if ($item instanceof Model) {
            return \think\model\Collection::make($resultSet);
        } else {
            return \think\Collection::make($resultSet);
        }
    }


    /**
     * 合并用户设置与默认数据
     */
    private function getMergeData($userData)
    {
        $defaultData = $this->defaultData();
        // 商城设置：配送方式
        if (isset($userData['store']['values']['delivery_type'])) {
            unset($defaultData['store']['values']['delivery_type']);
        }
        return array_merge_multiple($defaultData, $userData);
    }

    /**
     * 默认配置
     */
    public function defaultData($storeName = null)
    {
        return [
            'store' => [
                'key' => 'store',
                'describe' => '商城设置',
                'values' => [
                    // 网站状态
                    'web_status' => '0',//0开启1关闭
                    // 网站关闭后跳转url
                    'close_url' => '',
                    // 网站关闭后跳转静态页面
                    'close_html' => '',
                    // 商城名称
                    'web_name' => $storeName ?: '三勾官网',
                    // 网站LOGO
                    'web_logo' => '',
                    // 地址栏图标
                    'web_ico' => '',
                    // 网站网址
                    'web_site' => '',
                    // 首页标题
                    'web_title' => '',
                    // 关键词
                    'web_keywords' => '',
                    // 描述
                    'web_description' => '',
                    // 版权信息
                    'web_copyright' => '',
                    // 备案号
                    'web_recordnum' => '',
                    // 公安备案号
                    'web_garecordnum' => '',
                    // 第三方代码
                    'web_thirdcode_pc' => '',
                    // 是否记录日志
                    'is_get_log' => true
                ],
            ],
            'other' => [
                'key' => 'other',
                'describe' => '其它设置',
                'values' => [
                    // 地址
                    'address' => '湖北省武汉市某某科技园',
                    // 客服电话
                    'phone' => '400-123-4567',
                    // 邮箱
                    'email' => 'demo@163.com',
                    // 公众号
                    'wx_qrcode' => '',
                    // 地图经纬度
                    'coordinate' => '30.550139,114.139366',
                ],
            ],
            'storage' => [
                'key' => 'storage',
                'describe' => '上传设置',
                'values' => [
                    'default' => 'local',
                    'engine' => [
                        'local' => [],
                        'qiniu' => [
                            'bucket' => '',
                            'access_key' => '',
                            'secret_key' => '',
                            'domain' => 'http://'
                        ],
                        'aliyun' => [
                            'bucket' => '',
                            'access_key_id' => '',
                            'access_key_secret' => '',
                            'domain' => 'http://'
                        ],
                        'qcloud' => [
                            'bucket' => '',
                            'region' => '',
                            'secret_id' => '',
                            'secret_key' => '',
                            'domain' => 'http://'
                        ],
                    ]
                ],
            ],
        ];
    }

}
